package com.ruoyi.quartz.task;

import com.google.gson.JsonObject;
import com.ruoyi.app.home.entity.LoveMessage;
import com.ruoyi.app.home.mapper.SysLoveMessageMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DeepSeekUtil;
import com.ruoyi.common.websocket.handler.NotificationWebSocketHandler;
import com.ruoyi.config.NTFYTopicEnum;
import com.ruoyi.entity.MessageRequest;
import com.ruoyi.feign.NtfyServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("push")
@Slf4j
public class WebSocketTask {
    @Autowired
    SysLoveMessageMapper loveMessageMapper;
    @Autowired
    DeepSeekUtil deepSeekUtil;

    @Autowired
    NtfyServiceClient ntfyServiceClient;

    public void sendDailyNotification() {
        log.info("开始推送每日任务提醒");
        // 构造通知消息
        JsonObject notification = new JsonObject();
        String title = "糖宝，今天的情话来啦~ 💕";
        notification.addProperty("type", "notification");
        notification.addProperty("title", title);
        String message = deepSeekUtil.generateLoveMessage();
        String formattedMessage =
                "❤️ 亲爱的糖宝：\n\n" +
                message +
                "\n\n☀️ 今天也要开开心心的哦~\n\n" +
                "—— 永远爱你的老公";
        log.info("情话：{}", message);
        notification.addProperty("content", formattedMessage);
        notification.addProperty("time", System.currentTimeMillis());
        // 通过 WebSocket 广播消息
        NotificationWebSocketHandler.broadcastMessage(notification.toString());

        MessageRequest ntfyMessage = MessageRequest.builder().
                topic(NTFYTopicEnum.LOVE.getCode()).
                toMattermost(false).
                title(title).
                type("ntfy").
                content(formattedMessage).build();
        ntfyServiceClient.sendNtfyMessage(ntfyMessage);
    }
}
